﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.InformationModules
{
    public class InformationUpdaterRegister
    {
        public SystemDataUpdater<InformationModel> GetInformationUpdater()
        {
            SystemDataUpdater<InformationModel> systemDataUpdater = new SystemDataUpdater<InformationModel>();
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataDeleting += NotSupported;
            return systemDataUpdater;
        }

        private void NotSupported(object sender, DataValidationEventArgs<InformationModel> e)
        {
            e.Cancle = true;
            e.Message = "系统信息不允许删除";
        }

        private void SingletonValid(object sender, DataValidationEventArgs<InformationModel> e)
        {
            InformationFetcher informationFetcher = new InformationFetcher();
            if (e.Insert.AutoId == 0)
            {
                e.Message = String.Format("企业[{0}]的信息已经存在,不允许新增新内容",e.Insert.Company);
                return;
            }
        }

        private void DataNotSpaceValid(object sender, DataValidationEventArgs<InformationModel> e)
        {
            if(String.IsNullOrWhiteSpace(e.Insert.Company))
            {
                e.Message = "企业名称不能为空";
                e.Cancle = true;
                return;
            }
            if(String.IsNullOrWhiteSpace(e.Insert.Version))
            {
                e.Message = "版本名称不能为空";
                e.Cancle = true;
                return;
            }
            if(String.IsNullOrWhiteSpace(e.Insert.Address))
            {
                e.Message = "公司地址不能为空";
                e.Cancle = true;
                return;
            }
            if(String.IsNullOrWhiteSpace(e.Insert.License))
            {
                e.Message = "License不能为空";
                e.Cancle = true;
                return;
            }
            
        }
    }
}
